package cc.comac.mom.datahandlers.entity.restreceive;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import com.alibaba.ans.shaded.com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cc.comac.mom.dao.entity.QualityQarFrrRelationDao;
import cc.comac.mom.dao.entity.QualitySapPurchaseOrderDao;
import cc.comac.mom.entity.QualityQarCheckInvoiceEntity;
import cc.comac.mom.entity.QualityQarFrrRelationEntity;
import cc.comac.mom.entity.QualitySapPurchaseOrderEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.TimeUtil;

@Component
@Transactional
public class QualityQarFrrRelationDataHandler {

	@Autowired
	private QualityQarFrrRelationDao qualityQarFrrRelationDao;
	@Autowired
	private QualitySapPurchaseOrderDao qualitySapPurchaseOrderDao;
	@Autowired
	private BaseEntityDataUtil baseEntityDataUtil;
	@Autowired
    private RestTemplate restTemplate;
	public String openFrr(QualityQarFrrRelationEntity qualityQarFrrRelationEntity) {
		Map<String,String> map = new HashMap<String, String>();
		map.put("status", "1");
		map.put("msg", "");
		if (StringUtils.isEmpty(qualityQarFrrRelationEntity.getFrrNo())) {
			map.put("status", "0");
			map.put("msg", "frr编号不能为空");
			return map.toString();
		} else if (StringUtils.isEmpty(qualityQarFrrRelationEntity.getQarNo())) {
			map.put("status", "0");
			map.put("msg", "qar编号不能为空");
			return map.toString();
		}
		QueryWrapper<QualityQarFrrRelationEntity> queryWrapper = new QueryWrapper<QualityQarFrrRelationEntity>();
		queryWrapper.eq("qar_no", qualityQarFrrRelationEntity.getQarNo());
		queryWrapper.eq("frr_no", qualityQarFrrRelationEntity.getFrrNo());
		QualityQarFrrRelationEntity selectOne = qualityQarFrrRelationDao.selectOne(queryWrapper);
		if (selectOne != null) {
			map.put("status", "0");
			map.put("msg", "qar编号 "+qualityQarFrrRelationEntity.getQarNo()+" frr编号 "+qualityQarFrrRelationEntity.getFrrNo()+" 已存在！");
			return map.toString();
		}
		long id = baseEntityDataUtil.getId();
		qualityQarFrrRelationEntity.setId(id);
		int insert = qualityQarFrrRelationDao.insert(qualityQarFrrRelationEntity);
		if (insert != 1) {
			map.put("status", "0");
			map.put("msg", "新增失败");
			return map.toString();
		}
		return map.toString();
	}

	@SuppressWarnings("null")
	public String frrToFileOrScrap(QualityQarFrrRelationEntity qualityQarFrrRelationEntity) {
		Map<String,String> map = new HashMap<String, String>();
		map.put("status", "1");
		map.put("msg", "");
		if (StringUtils.isEmpty(qualityQarFrrRelationEntity.getFrrNo())) {
			map.put("status", "0");
			map.put("msg", "frr编号不能为空");
			return map.toString();
		} else if (StringUtils.isEmpty(qualityQarFrrRelationEntity.getQarNo())) {
			map.put("status", "0");
			map.put("msg", "qar编号不能为空");
			return map.toString();
		}
		QueryWrapper<QualityQarFrrRelationEntity> queryWrapper = new QueryWrapper<QualityQarFrrRelationEntity>();
		queryWrapper.eq("qar_no", qualityQarFrrRelationEntity.getQarNo());
		queryWrapper.eq("frr_no", qualityQarFrrRelationEntity.getFrrNo());
		QualityQarFrrRelationEntity selectOne = qualityQarFrrRelationDao.selectOne(queryWrapper);
		if (selectOne == null) {
			map.put("status", "0");
			map.put("msg", "qar编号 "+qualityQarFrrRelationEntity.getQarNo()+" frr编号 "+qualityQarFrrRelationEntity.getFrrNo()+" 不存在！");
			return map.toString();
		}
		selectOne.setStatus(qualityQarFrrRelationEntity.getStatus());
		selectOne.setLastModifiedTime(qualityQarFrrRelationEntity.getInspectDate());
		selectOne.setLastModifier(qualityQarFrrRelationEntity.getInspect());
		int updateById = qualityQarFrrRelationDao.updateById(selectOne);
		if (updateById != 1) {
			map.put("status", "0");
			map.put("msg", "更新失败！");
			return map.toString();
		}
		return map.toString();
		
	}

	public String sentQarDataToFrr(QualityQarCheckInvoiceEntity qualityQarCheckInvoiceEntity) {
		
		QueryWrapper<QualitySapPurchaseOrderEntity> queryWrapper = new QueryWrapper<QualitySapPurchaseOrderEntity>();
		queryWrapper.eq("purchase_order", qualityQarCheckInvoiceEntity.getPurchaseOrder());
		queryWrapper.eq("material", qualityQarCheckInvoiceEntity.getMaterialNumber());
		QualitySapPurchaseOrderEntity selectOne = qualitySapPurchaseOrderDao.selectOne(queryWrapper);
		
		HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("qarNo", qualityQarCheckInvoiceEntity.getQarNo());
        map.put("partCde", qualityQarCheckInvoiceEntity.getPartNo());
        map.put("serialNum", qualityQarCheckInvoiceEntity.getSerialNumber());
        map.put("batch", qualityQarCheckInvoiceEntity.getStoveNo());
        map.put("supplier", selectOne.getVendorName());
        map.put("version", selectOne.getBanCi());
        map.put("MaterialNum", qualityQarCheckInvoiceEntity.getMaterialNumber());
        map.put("SAPbatch", selectOne.getBatchNumber());
        map.put("inspect", baseEntityDataUtil.getCurrentUser());
        map.put("inspectDate", TimeUtil.getCurrentDateString());
        String jsonData=JSONObject.toJSONString(map);
        HttpEntity<String> requestEntity = new HttpEntity<String>(jsonData, headers);
        //发送请求，设置请求返回数据格式为String
        ParserConfig.getGlobalInstance().addAccept("com.jackrain.nea.exception.NDSException"); 
//       Object postForObject = restTemplate.postForObject("http://localhost:9099/p/cs/QualityFrrFormController/reciveFrrPartData", requestEntity, Object.class);
      Object postForObject = restTemplate.postForObject("http://localhost:9099/p/cs/QualityFrrFormController/openFrr", requestEntity, Object.class);
//       System.out.println(parseObject.get("status").toString() + "=======<<<<<<<>>>>>>>"+parseObject.get("msg").toString());
       return null;
	}
	
	
}
